package com.project.action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.List;

import org.json.JSONObject;
import org.json.JSONArray;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.project.bean.GetDirectAccount;
import com.project.bean.SortSingleAccountByAccount;
import com.project.bean.SortSingleAccountByFund;

import com.project.dao.implement.SortSingleAccountImpl;

public class SortSingleAccountServlet extends HttpServlet {

	/**
	 * Constructor of the object.
	 */
	public SortSingleAccountServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * The doGet method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to get.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html");
		PrintWriter out = response.getWriter();
		out.println("<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01 Transitional//EN\">");
		out.println("<HTML>");
		out.println("  <HEAD><TITLE>A Servlet</TITLE></HEAD>");
		out.println("  <BODY>");
		out.print("    This is ");
		out.print(this.getClass());
		out.println(", using the GET method");
		out.println("  </BODY>");
		out.println("</HTML>");
		out.flush();
		out.close();
	}

	/**
	 * The doPost method of the servlet. <br>
	 *
	 * This method is called when a form has its tag value method equals to post.
	 * 
	 * @param request the request send by the client to the server
	 * @param response the response send by the server to the client
	 * @throws ServletException if an error occurred
	 * @throws IOException if an error occurred
	 */
	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {

		response.setContentType("text/html/json;charset=utf-8");
		request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        
        String userName = request.getParameter("username");
        String userBankAccount = request.getParameter("userBankAccount");
        String userCreateActBank = request.getParameter("userCreateActBank");
        String sortMainType = request.getParameter("sortMainType");
        String sortSubType = request.getParameter("sortSubType");
        
        Map<String,Object> param = new HashMap<String,Object>();
    	param.put("userAccountNum", userBankAccount);
    	param.put("username", userName);
    	param.put("userCreateActBank", userCreateActBank);
        
    	SortSingleAccountImpl sortSingleMapper = new SortSingleAccountImpl();
    	List<SortSingleAccountByAccount> sortAccount = null;
    	List<SortSingleAccountByFund> sortFund = null;
    	
        if (sortMainType.equals("对方帐户出现频率"))
        {
        	if (sortSubType.equals("收入类交易"))
        	{
        		sortAccount = sortSingleMapper.GetTop20SameAccountCountByReceiver(param);
        	}
        	else if (sortSubType.equals("支付类交易"))
        	{
        		sortAccount = sortSingleMapper.GetTop20SameAccountCountByPayer(param);
        	}
        	else if (sortSubType.equals("全部交易"))
        	{
        		sortAccount = sortSingleMapper.GetTop20SameAccountCountByTotal(param);
        	}
        	else
        	{
        		//error message
        	}
        }
        else if (sortMainType.equals("相同交易金额频率"))
        {
        	if (sortSubType.equals("收入类交易"))
        	{
        		sortFund = sortSingleMapper.GetTop20SameFundCountByReceiver(param);
        	}
        	else if (sortSubType.equals("支付类交易"))
        	{
        		sortFund = sortSingleMapper.GetTop20SameFundCountByPayer(param);
        	}
        	else if (sortSubType.equals("全部交易"))
        	{
        		sortFund = sortSingleMapper.GetTop20SameFundCountByTotal(param);
        	}
        	else
        	{
        		//error message
        	}
        }
        else
        {
        	//error message
        }
        
        JSONObject json = new JSONObject(); 
        
        if (sortFund == null && sortAccount == null)
        {
        	try{
        		json.put("error", "没有查询到相关数据!");
        	}
        	catch(Exception e){               
        		e.printStackTrace();           
        	}   
        }
        
        if (sortAccount != null)
        {
        	try{    
        		JSONArray jsonArray =new JSONArray();
        		JSONObject member = null; 
        		
        		for (SortSingleAccountByAccount item : sortAccount) {
        			String account = item.getAccount();
        			long count = item.getCount();
        		    
        		    member = new JSONObject();
        			member.put("account", account);
        			member.put("count", count);

        			jsonArray.put(member);  
        		}
        		json.put("account", 1);
        		json.put("jsonArray", jsonArray);
        	}
        	catch(Exception e){               
        		e.printStackTrace();           
        	}           
        }
        
        if (sortFund!= null)
        {
        	try{    
        		JSONArray jsonArray =new JSONArray();
        		JSONObject member = null; 
        		
        		for (SortSingleAccountByFund item : sortFund) {
        			float fund = item.getFund();
        			long count = item.getCount();
        		    
        		    member = new JSONObject();
        			member.put("fund", fund);
        			member.put("count", count);

        			jsonArray.put(member);  
        		}
        		json.put("fund", 1);
        		json.put("jsonArray", jsonArray);
        	}
        	catch(Exception e){               
        		e.printStackTrace();           
        	} 
        }
        
    	response.getWriter().write(json.toString());  
    	response.getWriter().close();
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init() throws ServletException {
		// Put your code here
	}

}
